package com.tsinghuabigdata.edu.ddmath.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.tsinghuabigdata.edu.ddmath.R;


/**
 * Created by 28205 on 2016/12/15.
 */
public class LoadingView extends LoadFaultView {

    private View loadingView;
    private ImageView gifview;

    private AnimationDrawable animationDrawable;

    public LoadingView(Context context) {
        super(context);
        initialize();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    /**
     * 初始化界面元素
     */
    private void initialize() {
        loadingView = LayoutInflater.from(getContext()).inflate(R.layout.layout_loading, this, false);
        gifview = (ImageView) loadingView.findViewById(R.id.giv_loading);

        addView(loadingView, getChildCount());

        animationDrawable = (AnimationDrawable) gifview.getDrawable();
        startAnim();

    }

    private void startAnim() {
        animationDrawable.start();
    }

    private void stopAnim() {
        animationDrawable.stop();
    }

    public void showLoading() {
        //显示加载页，隐藏错误和空数据页面
        loadingView.setVisibility(VISIBLE);
        super.hide();
    }

    public void showFaultView(boolean isShowEmpty) {
        setVisibility(VISIBLE);
        loadingView.setVisibility(GONE);
        super.show(isShowEmpty);
    }

    public void hideall() {
        setVisibility(GONE);
    }
    public void hide(){
        loadingView.setVisibility(GONE);
        super.hide();
    }
}
